package com.feihong.yw189.bluetooth.startwritecard;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;

import com.feihong.yw189.bluetooth.listener.ReadIccidListener;
import com.feihong.yw189.bluetooth.listener.WriteCardListener;
import com.feihong.yw189.bluetooth.utils.djlm.DJLMCallBack;
import com.feihong.yw189.bluetooth.utils.djlm.DJLM_ReadICCIDAndSIM;
import com.feihong.yw189.bluetooth.utils.djlm.DJLM_WriteCard;


/**
 * 店家联盟写卡
 */

public class DjlmWrite {
    private String blueAddress;
    private Activity mActivity;
    private ReadIccidListener mReadIccidListener;
    private WriteCardListener mWriteCardListener;
    private DJLM_ReadICCIDAndSIM djlm_readICCID;
    private int mType;


    public DjlmWrite(Activity context, String blueAddress, int mType, ReadIccidListener readIccidListener
            , WriteCardListener writeCardListener) {
        this.mActivity = context;
        this.blueAddress = blueAddress;
        this.mType = mType;
        this.mReadIccidListener = readIccidListener;
        this.mWriteCardListener = writeCardListener;

    }

    //******************************************读iccid************************************************
    public void connect() {
        mActivity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                BLEReadOnce(blueAddress);
            }
        });
    }

    private void BLEReadOnce(String strAddress) {
        BluetoothAdapter ba = BluetoothAdapter.getDefaultAdapter();
        if (ba.isDiscovering()) {
            ba.cancelDiscovery();
        }

        djlm_readICCID = new DJLM_ReadICCIDAndSIM(mActivity, strAddress, new DJLMCallBack() {
            @Override
            public void success(String iccic) {
                if (iccic != null && iccic.trim().length() > 0 && iccic.indexOf("|") > 0) {
                    String[] args = iccic.split("\\|");
                    if (mType == 0) {
                        if (args[1].contains("FFF")) {
                            mReadIccidListener.readIccidIsChengKa(1617, args[0], "此卡是白卡,无需重置");
                        } else {
                            mReadIccidListener.readIccidSuc(4, args[0], args[1]);
                        }
                    } else {
                        if (!args[1].contains("FFF")) {
                            mReadIccidListener.readIccidIsChengKa(1617, args[0], "此卡是成卡,请换卡重试");
                        } else {
                            mReadIccidListener.readIccidSuc(4, args[0], args[1]);
                        }
                    }
                } else {
                    mReadIccidListener.readIccidErr("读取ICCID失败,请重试");
                }
            }

            @Override
            public void error(String message) {

                mReadIccidListener.readIccidErr("读取ICCID失败,请重试");
            }
        });
    }

    //************************************写卡*******************************
    public void writeCard(String IMSI, String Send_number) {
        int nRet = -1;
        try {
            DJLM_WriteCard xieka = new DJLM_WriteCard(mActivity, blueAddress, IMSI,
                    Send_number, new DJLMCallBack() {
                @Override
                public void success(String message) {
                    mWriteCardListener.writeSuc();
                }

                @Override
                public void error(String message) {
                    mWriteCardListener.writeErr();
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
            mWriteCardListener.writeErr();
            nRet = -1;
        }
    }

    /**
     * 关闭蓝牙连接
     */
    public void disConnect() {
        if (djlm_readICCID != null) {
            djlm_readICCID.disconnect();
        }
    }
}
